
package com.hlkj.pay.service.devices.impl;
/*
 * Hlpay-Plus aggregate payment system.
 * Copyright (c) 2024-2025 Hlpay Team Copyright has the right of final interpretation.
 */

import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hlkj.framework.common.pojo.PageResult;
import com.hlkj.framework.mybatis.core.query.QueryWrapperX;
import com.hlkj.framework.mybatis.core.utils.MyBatisUtils;
import com.hlkj.pay.dto.devices.MerchantQrDto;
import com.hlkj.pay.dto.devices.MerchantQrQueryDto;
import com.hlkj.pay.enums.CommonEnum;
import com.hlkj.pay.infrastructure.mapper.devices.MerchantQrMapper;
import com.hlkj.pay.infrastructure.mapper.devices.MerchantQrSetMapper;
import com.hlkj.pay.infrastructure.model.devices.MerchantQrDO;
import com.hlkj.pay.infrastructure.model.devices.MerchantQrSetDO;
import com.hlkj.pay.service.CommonSnFilterService;
import com.hlkj.pay.service.devices.IMerchantQrService;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * @author HlpayTeam
 * @date 2025/01/20 11:11
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class MerchantQrServiceImpl extends CommonSnFilterService implements IMerchantQrService {

    private final MerchantQrMapper merchantQrMapper;

    private final MerchantQrSetMapper merchantQrSetMapper;

    @Override
    public PageResult<MerchantQrDto> queryPage(MerchantQrQueryDto merchantQrQueryDto) {
        if (!StringUtils.hasText(merchantQrQueryDto.getSortingFields())) {
            merchantQrQueryDto.setSortingFields("a.id");
        }
        IPage<MerchantQrDto> mpPage = MyBatisUtils.buildPage(merchantQrQueryDto);
        QueryWrapper<MerchantQrDto> queryWrapper = buildInfoWrapper(merchantQrQueryDto);
        merchantQrMapper.selectQrPage(mpPage, queryWrapper);
        return new PageResult(mpPage.getRecords(), mpPage.getTotal(), mpPage.getCurrent(), mpPage.getSize(), mpPage.getPages());
    }

    @Override
    public void addMerchantQr(MerchantQrSetDO merchantQrSetDO, List<MerchantQrDO> merchantQrList) {
        merchantQrSetMapper.insert(merchantQrSetDO);
        merchantQrMapper.insertBatch(merchantQrList);
    }

    @Override
    public MerchantQrDO queryDetail(String code) {
        return merchantQrMapper.selectOne(MerchantQrDO::getCode, code);
    }

    @Override
    public void updateMerchantQr(MerchantQrDO merchantQrDO) {
        merchantQrMapper.updateById(merchantQrDO);
    }

    @Override
    public MerchantQrSetDO queryDetailSet(String batchNo) {
        return merchantQrSetMapper.selectOne(MerchantQrSetDO::getBatchNo, batchNo);
    }

    /**
     * 查询条件封装
     *
     * @param merchantQrQueryDto
     * @return
     */
    QueryWrapper<MerchantQrDto> buildInfoWrapper(MerchantQrQueryDto merchantQrQueryDto) {
        filterSn(merchantQrQueryDto);
        QueryWrapperX<MerchantQrDto> queryWrapper = new QueryWrapperX<>();
        queryWrapper.eq("a.delete_flag", CommonEnum.YES_NO_TYPE.NO.getCode());
        queryWrapper.likeIfPresent("a.batch_no", merchantQrQueryDto.getBatchNo());
        queryWrapper.eqIfPresent("a.sn", merchantQrQueryDto.getSn());
        queryWrapper.inIfPresent("a.sn", merchantQrQueryDto.getSnList());
        queryWrapper.eqIfPresent("a.template_id", merchantQrQueryDto.getTemplateId());
        queryWrapper.likeIfPresent("a.app_id", merchantQrQueryDto.getAppId());
        queryWrapper.geIfPresent("a.create_time", merchantQrQueryDto.getStartQueryTime());
        queryWrapper.leIfPresent("a.create_time", merchantQrQueryDto.getEndQueryTime());
        queryWrapper.likeIfPresent("info.super_agent", merchantQrQueryDto.getAgentId());
        return queryWrapper;
    }
}
